package com.his.his_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.his_system.common.exception.DisableStateException;
import com.his.his_system.common.exception.PasswordBadException;
import com.his.his_system.domain.DzmHisMember;
import com.his.his_system.domain.LoginUser;
import com.his.his_system.service.DzmHisMemberService;
import com.his.his_system.mapper.DzmHisMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Objects;


/**
* @author 赵康
* @description 针对表【dzm_his_member(HIS用户表)】的数据库操作Service实现
* @createDate 2023-07-03 14:27:13
*/
@Service
public class DzmHisMemberServiceImpl extends ServiceImpl<DzmHisMemberMapper, DzmHisMember>
    implements DzmHisMemberService , UserDetailsService {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private DzmHisMemberMapper dzmHisMemberMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;// =new BCryptPasswordEncoder();

    @Override
    public DzmHisMember selectByUserName(String userName) {
        return dzmHisMemberMapper.selectByUserName(userName);
    }

    @Override
    public boolean changePswd(DzmHisMember dzmHisMember) throws PasswordBadException {

//        判断旧密码是否正确


        QueryWrapper<DzmHisMember> qr =
                new QueryWrapper<DzmHisMember>()
                        .select("password")
                        .eq("uid",dzmHisMember.getUid());
        DzmHisMember hisMember = dzmHisMemberMapper.selectOne(qr);
        if (!passwordEncoder.matches(dzmHisMember.getPassword(),hisMember.getPassword())) {
            throw new PasswordBadException("密码不正确");
        }
        UpdateWrapper<DzmHisMember> ur =
                new UpdateWrapper<DzmHisMember>().eq("uid", dzmHisMember.getUid());
//        加密
        dzmHisMember.setPassword(passwordEncoder.encode(dzmHisMember.getNewPassword()));
        int update = dzmHisMemberMapper.update(dzmHisMember, ur);


        return update==1;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String ip = request.getRemoteAddr();
        System.out.println(ip);

        System.out.println("查询数据库验证用户名密码");
        DzmHisMember user = dzmHisMemberMapper.selectByUserName(username);

        if (Objects.isNull(user)) {
            throw new RuntimeException("用户名或密码错误");
        }

        if(user.getStatus()==2)throw new DisableStateException("用户已被禁用");

        return user;
    }
}




